package com.wzx.ptplatform.controller.admin.user;

import com.wzx.ptplatform.domain.*;
import com.wzx.ptplatform.service.ProjectExperienceService;
import com.wzx.ptplatform.service.StudentService;
import com.wzx.ptplatform.service.WorkExperienceService;
import com.wzx.ptplatform.utils.ConstUtils;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员用户模块——学生信息管理")
@RestController
public class AdminUserStudentController {

    final StudentService studentService;
    final WorkExperienceService workExperienceService;
    final ProjectExperienceService projectExperienceService;

    public AdminUserStudentController(StudentService studentService, WorkExperienceService workExperienceService, ProjectExperienceService projectExperienceService) {
        this.studentService = studentService;
        this.workExperienceService = workExperienceService;
        this.projectExperienceService = projectExperienceService;
    }

    @RequestMapping("admin_user_student_message_list")
    public LayuiPager<Student> adminUserStudentMessageList(
            String studentName, String universityName, String majorName, String cityName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        Student student = new Student();
        if (studentName != null && !"".equals(studentName)) {
            student.setStudentName(studentName);
        }
        if (universityName != null && !ConstUtils.PLEASE_CHOOSE.equals(universityName)) {
            student.setUniversity(new University(Long.parseLong(universityName)));
        }
        if (majorName != null && !ConstUtils.PLEASE_CHOOSE.equals(majorName)) {
            student.setMajor(new Major(Long.parseLong(majorName)));
        }
        if (cityName != null && !ConstUtils.PLEASE_CHOOSE.equals(cityName)) {
            student.setCity(new City(Long.parseLong(cityName)));
        }
        Page<Student> pager = studentService.findList(student, page - 1, limit);
        LayuiPager<Student> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_user_student_message_addView")
    public ModelAndView adminUserStudentMessageAddView() {
        return new ModelAndView("admin/user/student_message_add");
    }

    @GetMapping("admin_user_student_message_editView")
    public ModelAndView adminUserStudentMessageEditView(String id, Model model) {
        model.addAttribute("student", studentService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/user/student_message_edit");
    }

    @PostMapping("admin_user_student_message_save_or_edit")
    public Map<String, Object> adminUserStudentMessageSaveOrEdit(Student student) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (studentService.saveOrEdit(student)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_user_student_message_delete")
    public Map<String, Object> adminUserStudentMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (studentService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_user_student_message_delete_select")
    public Map<String, Object> adminUserStudentMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    studentService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }

    @GetMapping("admin_user_student_resume_message")
    public ModelAndView adminUserStudentResumeMessage(String id, Model model) {
        Student student = studentService.findById(Long.parseLong(id));
        System.out.println(student);
        List<WorkExperience> workExperienceList;
        List<ProjectExperience> projectExperienceList;
        if (student.getResume() != null) {
            workExperienceList = workExperienceService.findList(new WorkExperience(student.getResume()), null, null).getContent();
            projectExperienceList = projectExperienceService.findList(new ProjectExperience(student.getResume()), null, null).getContent();
        } else {
            student.setResume(new Resume("", new Industry(), new Job(), new City(), null, null, ""));
            workExperienceList = Collections.singletonList(new WorkExperience("", new Job(), new City(), null, null, ""));
            projectExperienceList = Collections.singletonList(new ProjectExperience("", "", ""));
        }
        model.addAttribute("student", student);
        model.addAttribute("workExperienceList", workExperienceList);
        model.addAttribute("projectExperienceList", projectExperienceList);
        return new ModelAndView("admin/user/student_resume_message");
    }
}
